﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Recon.Portal.Business;
using Recon.Portal.DataObjects;
using Recon.Portal.Data;

namespace Recon.Portal.WebUI.Modules
{
    public partial class RemitterAccountMaster : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (((User)Session["CurrentUserVO"]) == null)
            {
                Response.Redirect("ReconSignIn.aspx");
            }
            if (!Page.IsPostBack)
            {
                //FillCustomerForMatching();
                FillRemitter();
                BindCustomerAccountData();
                lblCustmerName.Text = ((User)Session["CurrentUserVO"]).CustomerID + " - " + ((User)Session["CurrentUserVO"]).CustomerName;
                FillBank();
            }
        }

        private void BindCustomerAccountData()
        {
            RemitAccountDAO accountDAO = DAOFactory.GetRemitAccountDAO();
            List<RemitAccount> accountLst = accountDAO.GetAllRemitterAccountsForCustomer(Convert.ToString(((User)Session["CurrentUserVO"]).CustomerID));
                
            try
            {
                if (accountLst != null)
                {
                    gvCustAccount.DataSource = accountLst;
                    gvCustAccount.DataBind();
                }
                else
                {
                    gvCustAccount.DataSource = null;
                    gvCustAccount.DataBind();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        private void FillRemitter()
        {
            RemitterEntity lstEntity = new RemitterEntity();
            List<Remitter> dtRemitter = lstEntity.GetRemitterIdForMatchingCriteria(((User)Session["CurrentUserVO"]).CustomerID);

            if (dtRemitter != null && dtRemitter.Count<Remitter>() > 0)
            {
                foreach (Remitter remitter in dtRemitter)
                {
                    ddlRemitter.Items.Add(new ListItem(remitter.RemitterID + " - " + remitter.RemitterName, remitter.RemitterID));
                }
            }
        }

        private void FillBank()
        {
            BankDAO bankDAO = DAOFactory.getBankDAO();
            List<Bank> bankLst = bankDAO.getAllBanks();
            if (bankLst != null && bankLst.Count > 0)
            {
                for (int i = 0; i < bankLst.Count; i++)
                {
                    ddlBankId.Items.Add(bankLst[i].BankID.Trim() + " - " + bankLst[i].BankName.Trim());
                    ddlBankId.Items[i + 1].Value = (bankLst[i].BankID.Trim());
                }
            }
        }

        private void ResetFields()
        {
            ddlRemitter.SelectedValue = "0";
            ddlBankId.SelectedValue = "0";
            txtAccountName.Text = "";
            txtAccountNumber.Text = "";
            pnlRemitter.Visible = true;
            pnlRemitter.Enabled = true;
            btnSubmit.Text = "Add";
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            int retval = 0;
            RemitAccount account = null;
            if (ddlRemitter.SelectedValue != "0")
            {
                RemitAccountDAO accountDAO = DAOFactory.GetRemitAccountDAO();
                List<RemitAccount> accountLst = accountDAO.GetAllRemitAccounts(Convert.ToString(ddlRemitter.SelectedValue.Trim()), Convert.ToString(txtAccountNumber.Text.Trim()), Convert.ToString(((User)Session["CurrentUserVO"]).CustomerID));
                if (accountLst != null)
                {
                    account = new RemitAccount();
                    account.CustID = Convert.ToString(((User)Session["CurrentUserVO"]).CustomerID);
                    account.RemitID = Convert.ToString(ddlRemitter.SelectedValue).Trim();
                    account.AccountNumber = Convert.ToString(txtAccountNumber.Text.Trim());
                    account.AccountName = Convert.ToString(txtAccountName.Text.Trim());
                    account.BankID = Convert.ToString(ddlBankId.SelectedValue.Trim());

                    retval = accountDAO.Update(account);

                    if (retval != 0)
                    {
                        lblMessage.Text = "Remitter account modified successfully!";
                        btnClickPopup.Show();
                    }
                    else
                    {
                        lblMessage.Text = "Error in modify remitter account detail!";
                        btnClickPopup.Show();
                    }
                    //lblMessage.Text = "Account number for this remitter already exist!";
                    //btnClickPopup.Show();
                }
                else
                {
                    account = new RemitAccount();

                    account.RemitID = Convert.ToString(ddlRemitter.SelectedValue).Trim();
                    account.AccountNumber = Convert.ToString(txtAccountNumber.Text.Trim());
                    account.AccountName = Convert.ToString(txtAccountName.Text.Trim());
                    account.BankID = Convert.ToString(ddlBankId.SelectedValue.Trim());
                    account.CustID = Convert.ToString(((User)Session["CurrentUserVO"]).CustomerID);
                    retval = accountDAO.Insert(account);
                    if (retval != 0)
                    {
                        lblMessage.Text = "Remitter account added successfully!";
                        btnClickPopup.Show();
                        ResetFields();
                        BindCustomerAccountData();
                    }

                    else
                    {
                        lblMessage.Text = "Error adding remitter account detail!";
                        btnClickPopup.Show();
                    }
                }
                BindCustomerAccountData();
            }
            else
            {
                lblMessage.Text = "Please select remitter Id!";
                btnClickPopup.Show();
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            ResetFields();
        }

        protected void btnok_Click(object sender, EventArgs e)
        {
            btnClickPopup.Hide();
        }

        //protected void gvCustAccount_RowEditing(object sender, GridViewEditEventArgs e)
        //{
        //    gvCustAccount.EditIndex = e.NewEditIndex;
        //    BindCustomerAccountData();
        //}

        //protected void gvCustAccount_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        //{
        //    gvCustAccount.EditIndex = -1;
        //    BindCustomerAccountData();
        //}

        //protected void gvCustAccount_RowUpdating(object sender, GridViewUpdateEventArgs e)
        //{
        //    int retval = 0;
        //    try
        //    {
        //        RemitAccountDAO accountDAO = DAOFactory.GetRemitAccountDAO();
                
        //        RemitAccount account = new RemitAccount();
        //        account.CustID = Convert.ToString(gvCustAccount.Rows[e.RowIndex].Cells[1].Text);
        //        account.RemitID = Convert.ToString(gvCustAccount.Rows[e.RowIndex].Cells[2].Text);
        //        account.AccountNumber = Convert.ToString(gvCustAccount.Rows[e.RowIndex].Cells[3].Text);
        //        account.AccountName = Convert.ToString(((TextBox)gvCustAccount.Rows[e.RowIndex].Cells[4].Controls[0]).Text);
        //        account.BankID = Convert.ToString(gvCustAccount.Rows[e.RowIndex].Cells[5].Text);
                
        //        retval = accountDAO.Update(account);

        //        if (retval != 0)
        //        {
        //            lblMessage.Text = "Remitter account modified successfully!";
        //            btnClickPopup.Show();
        //        }
        //        else
        //        {
        //            lblMessage.Text = "Error in modify remitter account detail!";
        //            btnClickPopup.Show();
        //        }
        //    }
        //    catch
        //    {

        //    }
        //    gvCustAccount.EditIndex = -1;
        //    BindCustomerAccountData();

        //}

        protected void gvCustAccount_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int retval = 0;
            try
            {
                RemitAccountDAO accountDAO = DAOFactory.GetRemitAccountDAO();
                RemitAccount account = new RemitAccount();
                Label custId = ((Label)gvCustAccount.Rows[e.RowIndex].Cells[1].FindControl("lblCustomerID"));
                Label remitId = ((Label)gvCustAccount.Rows[e.RowIndex].Cells[2].FindControl("lblRemitID"));
                Label accNo = ((Label)gvCustAccount.Rows[e.RowIndex].Cells[3].FindControl("lblAccountNo"));
                
                account.CustID = Convert.ToString(custId.Text);
                account.RemitID = Convert.ToString(remitId.Text);
                account.AccountNumber = Convert.ToString(accNo.Text);

                retval = accountDAO.Delete(account); 
                    
                if (retval != 0)
                {
                    lblMessage.Text = "Remitter account deleted successfully!";
                    btnClickPopup.Show();
                }
                else
                {
                    lblMessage.Text = "Error in deleting remitter account detail!";
                    btnClickPopup.Show();
                }
            }
            catch
            {

            }
            BindCustomerAccountData();

        }
    }
}